<html>
<head><meta charset="utf-8"><title>Idiomacy of Arc&lt;RwLock&lt;T&gt;&gt; usage · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html">Idiomacy of Arc&lt;RwLock&lt;T&gt;&gt; usage</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218476584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218476584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218476584">(Dec 01 2020 at 20:26)</a>:</h4>
<p>This may be a little bit of a subjective topic but I still have to ask. Out of these two options, which one do you think is the more idiomatic?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">parking_lot</span>::<span class="n">RwLock</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Client</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">config</span>: <span class="nc">Arc</span><span class="o">&lt;</span><span class="n">RwLock</span><span class="o">&lt;</span><span class="n">Config</span><span class="o">&gt;&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Client</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">get</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="nb">FnOnce</span><span class="p">(</span><span class="n">config</span>: <span class="kp">&amp;</span><span class="nc">Config</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">read</span><span class="p">().</span><span class="n">deref</span><span class="p">())</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>

<span class="w">  </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">update</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="nb">FnOnce</span><span class="p">(</span><span class="n">config</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">Config</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">write</span><span class="p">().</span><span class="n">deref_mut</span><span class="p">())</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>

<span class="w">  </span><span class="k">fn</span> <span class="nf">somewhere</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="o">|</span><span class="n">cfg</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">cfg</span><span class="p">.</span><span class="n">some_field</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"new_value"</span><span class="p">;</span><span class="w"></span>
<span class="w">      </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>

<span class="w">  </span><span class="k">fn</span> <span class="nf">somewhere_else</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">some_field</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="o">|</span><span class="n">cfg</span><span class="o">|</span><span class="w"> </span><span class="n">cfg</span><span class="p">.</span><span class="n">some_field</span><span class="p">.</span><span class="n">clone</span><span class="p">());</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">parking_lot</span>::<span class="n">RwLock</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Client</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">config</span>: <span class="nc">Arc</span><span class="o">&lt;</span><span class="n">RwLock</span><span class="o">&lt;</span><span class="n">Config</span><span class="o">&gt;&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Client</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span> <span class="nf">somewhere</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">write</span><span class="p">().</span><span class="n">some_field</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"new_value"</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>

<span class="w">  </span><span class="k">fn</span> <span class="nf">somewhere_else</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">some_field</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">read</span><span class="p">().</span><span class="n">some_field_in_config</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I reckon the second approach has a higher chance of introducing deadlocks, but all the clones and closures in the first one bother me a little. And I guess it's also a little more readable?. Would you argue that any of those two is more idiomatic or preferred over the other?</p>



<a name="218477193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218477193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218477193">(Dec 01 2020 at 20:31)</a>:</h4>
<p>The <code>somewhere_else</code> fn shouldn't be legal, since you take a borrow to <code>some_field_in_config</code> for longer than <code>self.config.read()</code> lives.</p>
<p>I also would say the <code>update()</code> fn in the first option seems more likely to cause a deadlock, since that function needs to know that it takes the <code>config</code> lock, which is kind of implicit. Calling a function while holding a lock if that function isn't aware of it is an easy way to have a dead lock, since you could easily end up with taking locks in different orders.</p>



<a name="218477456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218477456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218477456">(Dec 01 2020 at 20:33)</a>:</h4>
<p>The first one isn't syntactically valid, so that's easy to rule out as a choice :-)</p>



<a name="218477547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218477547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218477547">(Dec 01 2020 at 20:33)</a>:</h4>
<p>And</p>
<div class="codehilite"><pre><span></span><code>no method named `deref` found for struct `parking_lot::lock_api::RwLockReadGuard&lt;&#39;_, parking_lot::RawRwLock, Config&gt;` in the current scope
</code></pre></div>



<a name="218477748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218477748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218477748">(Dec 01 2020 at 20:35)</a>:</h4>
<p>I don't see how the second example would be more likely to introduce deadlocks.</p>



<a name="218477808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218477808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218477808">(Dec 01 2020 at 20:35)</a>:</h4>
<p>nothing of complexity occurs while the reader / writer locks are held.</p>



<a name="218478671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218478671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218478671">(Dec 01 2020 at 20:43)</a>:</h4>
<p>isn't that just needing the <code>use std::ops::Deref</code> ? <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> <br>
I'm confused about your comment <span class="user-mention" data-user-id="209168">@Thom Chiovoloni</span> , it seems to be valid code for me <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4db16f76503be0634094d43710a1489b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4db16f76503be0634094d43710a1489b</a></p>



<a name="218478810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218478810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218478810">(Dec 01 2020 at 20:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="246514">Martin Asquino</span> <a href="#narrow/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage/near/218478671">said</a>:</p>
<blockquote>
<p>isn't that just needing the <code>use std::ops::Deref</code> ? <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> <br>
I'm confused about your comment <span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> , it seems to be valid code for me <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4db16f76503be0634094d43710a1489b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4db16f76503be0634094d43710a1489b</a></p>
</blockquote>
<p>Perhaps! My tolerance for spending my time guessing the missing parts of code from people asking for help is notably lower than other peoples.</p>



<a name="218478817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218478817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218478817">(Dec 01 2020 at 20:45)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span>  what I mean by more likely to introduce deadlocks I mean more likely to be overlooked by the dev</p>



<a name="218478873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218478873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218478873">(Dec 01 2020 at 20:45)</a>:</h4>
<p>ah fair enough, yeah I should have made that a playground from the get go <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="218479047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479047">(Dec 01 2020 at 20:46)</a>:</h4>
<p>Ah, I didn't realize the read lock guard's lifetime would be extended by the existence of the borrow. Huh.</p>



<a name="218479297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479297">(Dec 01 2020 at 20:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="246514">Martin Asquino</span> <a href="#narrow/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage/near/218478817">said</a>:</p>
<blockquote>
<p>by more likely to introduce deadlocks I mean more likely to be overlooked by the dev</p>
</blockquote>
<p>How would that lead to a deadlock in this case?</p>



<a name="218479338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479338">(Dec 01 2020 at 20:49)</a>:</h4>
<p>deadlocks can only happen when two threads are waiting for each other</p>



<a name="218479395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479395">(Dec 01 2020 at 20:49)</a>:</h4>
<p>but the second case nothing can happen while the lock is held</p>



<a name="218479629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479629">(Dec 01 2020 at 20:51)</a>:</h4>
<p>now, the first case seems more deadlock possible since you allow arbitrary functions to run while the lock is held</p>



<a name="218479950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479950">(Dec 01 2020 at 20:54)</a>:</h4>
<p>e.g.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">somewhere</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="o">|</span><span class="n">cfg</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">somewhere_else</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">cfg</span><span class="p">.</span><span class="n">some_field</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"new_value"</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218479982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218479982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218479982">(Dec 01 2020 at 20:54)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f5a989de93fbc7a52956a974fefa58b0">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f5a989de93fbc7a52956a974fefa58b0</a></p>



<a name="218480035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218480035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218480035">(Dec 01 2020 at 20:54)</a>:</h4>
<p>I've added the two options there, mixed in the same snippet though. And a deadlock function which deadlocks</p>



<a name="218480093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218480093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218480093">(Dec 01 2020 at 20:55)</a>:</h4>
<p>I reckon it's not extremely likely to happen, and that it should be easy enough to spot once you notice the deadlock, but I still wonder</p>



<a name="218480131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218480131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218480131">(Dec 01 2020 at 20:55)</a>:</h4>
<p>Save yourself some typing as well:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">get</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Config</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">read</span><span class="p">())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218480614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218480614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Martin Asquino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218480614">(Dec 01 2020 at 20:58)</a>:</h4>
<p>fwiw, the first option is on an open source project I'm contributing to, the second option is the one I use in some code I intend to merge, and I think it would do wonders for readability to change the other bits of code that use the first approach. But I wasn't sure if that was a common approach and I'm just being overly annoyed by the closures for no good reason <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span> (which I kinda am)</p>



<a name="218580061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218580061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218580061">(Dec 02 2020 at 16:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage/near/218479047">said</a>:</p>
<blockquote>
<p>Ah, I didn't realize the read lock guard's lifetime would be extended by the existence of the borrow. Huh.</p>
</blockquote>
<p>Same here; which is a reason I personally prefer the callback pattern, provided it is named in a way that conveys locking: <code>.with_shared_lock(|it| …)</code> <code>.with_unique_lock(|it| …)</code>.</p>



<a name="218583587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Idiomacy%20of%20Arc%3CRwLock%3CT%3E%3E%20usage/near/218583587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Idiomacy.20of.20Arc.3CRwLock.3CT.3E.3E.20usage.html#218583587">(Dec 02 2020 at 16:41)</a>:</h4>
<p><em>wrong topic</em></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>